package com.farsunset.ichat.cim.handler;

import org.apache.log4j.Logger;

import com.farsunset.cim.nio.constant.CIMConstant;
import com.farsunset.cim.nio.handler.CIMRequestHandler;
import com.farsunset.cim.nio.mutual.ReplyBody;
import com.farsunset.cim.nio.mutual.SentBody;
import com.farsunset.cim.nio.session.CIMSession;
import com.farsunset.cim.nio.session.SessionManager;
import com.farsunset.ichat.common.util.ContextHolder;

/**
 * 断开连接，清除session
 * 
 */
public class SessionClosedHandler implements CIMRequestHandler {

	protected final Logger logger = Logger.getLogger(SessionClosedHandler.class);

	public ReplyBody process(CIMSession ios, SentBody message) {

		ReplyBody reply = new ReplyBody();
		if (ios.getAttribute(CIMConstant.SESSION_KEY) == null) {
			reply.setCode(CIMConstant.ReturnCode.CODE_500);
			return reply;
		}

		String account = ios.getAttribute(CIMConstant.SESSION_KEY).toString();
		SessionManager sessionManager = ContextHolder.getBean(SessionManager.class);
		sessionManager.removeSession(account);
		return reply;
	} 
	
}